package com.sljy.hzk.net;



import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sljy.hzk.R;

import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * Created  on 2018-05-17
 * @author 唐开阔
 * @describe
 */
public abstract class BaseCallBack<T> implements Observer<CommonResult<T>> {
    //private MaterialDialog progressDialog;
    private Dialog progressDialog;
    private boolean isShowDialog;
    public BaseCallBack() {
        this(null);
    }

    public BaseCallBack(Context context) {
        this(context,"加载中请稍后");
    }

    public BaseCallBack(Context context, String loadingMes) {
        isShowDialog = context != null;
        if (isShowDialog){
//            progressDialog =  new MaterialDialog.Builder(context)
//                    .content(loadingMes)
//                    .iconRes(R.drawable.outerring)
//                    .progress(true, 0).build();

            progressDialog =new Dialog(context);//可以在style中设定dialog的样式
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setContentView(R.layout.dialog_loading);
            WindowManager.LayoutParams lp= progressDialog.getWindow().getAttributes();
            lp.gravity= Gravity.CENTER;
            lp.height= WindowManager.LayoutParams.WRAP_CONTENT;
            lp.width= WindowManager.LayoutParams.MATCH_PARENT;
            progressDialog.getWindow().setAttributes(lp);
            //设置该属性，dialog可以铺满屏幕
            progressDialog.getWindow().setBackgroundDrawable(null);

            progressDialog.setCancelable(false);
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (isShowDialog){
            progressDialog.show();
            ImageView load =(ImageView)progressDialog.getWindow().findViewById(R.id.loading);
            Animation ani = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            ani.setDuration(3000);//动画时间
            ani.setRepeatCount(-1);//动画的重复次数
            ani.setFillAfter(true);//设置为true，动画转化结束后被应用
            load.startAnimation(ani);//开始动=
        }
    }
    @Override
    public void onNext(CommonResult<T> tCommonResult) {
        if (isShowDialog){
            progressDialog.dismiss();
        }
        if (HttpConfig.RESPONSE_OK.equals(tCommonResult.getCode())){
            onCallBackSuccess(tCommonResult.getData());
        }else {
            onFail(tCommonResult.getMsg());
        }
    }
    @Override
    public void onError(Throwable e) {
        if (isShowDialog){
            progressDialog.dismiss();
        }
        if (e instanceof SocketTimeoutException){
            onFail("链接超时...请稍后再试");

        }else if (e instanceof java.net.ConnectException){
            onFail("网络链接失败...请稍后再试");

        }
        else {
            onFail(e.getMessage());
        }
    }
    @Override
    public void onComplete() {

    }
    /**
     * 数据请求成功
     * @param data
     */
    public abstract void onCallBackSuccess(T data);
    /**
     * 数据请求失败
     * @param mes
     */
    public  void onFail(String mes){};
}
